import java.util.Scanner;
import java.util.Random;
public class test{
    public static String encrypt(String input) {
    StringBuilder sb = new StringBuilder(input);
    for (int i = 0; i < sb.length(); i++) {
        char c = sb.charAt(i);
        c += i + 1;
        c += 3;
        sb.setCharAt(i, c);
    }
    char first = sb.charAt(0);
    char last = sb.charAt(sb.length() - 1);
    sb.setCharAt(0, last);
    sb.setCharAt(sb.length() - 1, first);
    return sb.reverse().toString();
}
    //解密功能

    public static String decrypt(String input) {
        StringBuilder sb = new StringBuilder(input).reverse();
        char first = sb.charAt(0);
        char last = sb.charAt(sb.length() - 1);
        sb.setCharAt(0, last);
        sb.setCharAt(sb.length() - 1, first);
        for (int i = 0; i < sb.length(); i++) {
            char c = sb.charAt(i);
            c -= 3;
            c -= i + 1;
            sb.setCharAt(i, c);
        }
        return sb.toString();
    }
    //判断密码强度

    public static int checkPasswordStrength(String password) {
        int length = password.length();
        boolean hasUpperCase = false;
        boolean hasLowerCase = false;
        boolean hasDigit = false;
        for (int i = 0; i < length; i++) {
            char c = password.charAt(i);
            if (Character.isUpperCase(c)) {
                hasUpperCase = true;
            } else if (Character.isLowerCase(c)) {
                hasLowerCase = true;
            } else if (Character.isDigit(c)) {
                hasDigit = true;
            }
        }
        if (length < 8 || (hasUpperCase && hasLowerCase && hasDigit)) {
            return 3; // 高强度
        } else if (length >= 8 && (hasUpperCase || hasLowerCase) && hasDigit) {
            return 2; // 中强度
        } else {
            return 1; // 弱强度
        }
    }
    //密码生成

    public static String generatePassword(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char c;
            int type = random.nextInt(3);
            switch (type) {
                case 0:
                    c = (char) ('0' + random.nextInt(10));
                    break;
                case 1:
                    c = (char) ('a' + random.nextInt(26));
                    break;
                default:
                    c = (char) ('A' + random.nextInt(26));
                    break;
            }
            sb.append(c);
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：");
            System.out.println("1.加密 2.解密 3.判断密码强度 4.密码生成");
            String choiceStr = scanner.nextLine();
            int choice;
            try {
                choice = Integer.parseInt(choiceStr);
            } catch (NumberFormatException e) {
                System.out.println("无效的选项，请输入数字1-4");
                continue;
            }
            if (choice < 1 || choice > 4) {
                System.out.println("无效的选项，请输入数字1-4");
                continue;
            }
            switch (choice) {
                case 1:
                    System.out.println("请输入要加密的字符串：");
                    String input = scanner.nextLine();
                    String encrypted = encrypt(input);
                    System.out.println("加密后的字符串为：" + encrypted);
                    break;
                case 2:
                    System.out.println("请输入要解密的字符串：");
                    input = scanner.nextLine();
                    String decrypted = decrypt(input);
                    System.out.println("解密后的字符串为：" + decrypted);
                    break;
                case 3:
                    System.out.println("请输入密码：");
                    String password = scanner.nextLine();
                    int strength = checkPasswordStrength(password);
                    switch (strength) {
                        case 1:
                            System.out.println("密码强度：弱");
                            break;
                        case 2:
                            System.out.println("密码强度：中");
                            break;
                        case 3:
                            System.out.println("密码强度：强");
                            break;
                    }
                    break;
                case 4:
                    System.out.println("请输入要生成密码的长度：");
                    String lengthStr = scanner.nextLine();
                    int length;
                    try {
                        length = Integer.parseInt(lengthStr);
                    } catch (NumberFormatException e) {
                        System.out.println("无效的长度，请输入数字");
                        continue;
                    }
                    if (length < 1 || length > 16) {
                        System.out.println("长度必须在1-16之间");
                        continue;
                    }
                    String generated = generatePassword(length);
                    System.out.println("生成的密码为：" + generated);
                    break;
            }
        }
    }
}
